/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modelo.dao;

import modelo.util.HibernateUtil;
import java.util.List;
import modelo.Aluno;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Leonardo
 */
public class AlunoDAO{
    
    private Session session;    
    private Transaction transaction;
    
    public AlunoDAO(){                
        HibernateUtil hu = new HibernateUtil(Aluno.class);
        session = hu.getSession();
        transaction = session.getTransaction();
    }    
    public void adicionar(Aluno a){        
        transaction.begin();
        session.save(a);        
        transaction.commit();
    }
    public void remover(Aluno a){        
        transaction.begin();
        session.delete(a);
        transaction.commit();
    }
    public void atualizar(Aluno a){        
        transaction.begin();
        session.update(a);
        transaction.commit();
    }
    public Aluno pesquisarPorId (long id){        
        return (Aluno) session.load(Aluno.class,id);
    }
    public List<Aluno> pesquisarPorNome (String nome){                
        Query q =  session.createQuery("select a from Aluno as a where a.nome = :anome");        
        q.setParameter("anome", nome);
        return q.list();
    }
    public List<Aluno> getListar(){        
        return session.createQuery("select a from Aluno as a ").list();
    }
}

